﻿using D.Medicalcare.Domain.Entities;
using D.Medicalcare.ErrorCode;
using D.Medicalcare.Infrastructure.Interfaces;
using D.Medicalcare.Read.API.Application.Command.PhysicalexaminationCommand;
using D.Medicalcare.Read.API.Dtos;
using MediatR;
using Microsoft.IdentityModel.Tokens;

namespace D.Medicalcare.Read.API.Application.Handler.PhysicalexaminationHarder
{
    public class GetPhysicalexaminationHarder : IRequestHandler<GetPhysicalexaminationCommand, APIPaging<PhysicalexaminationDto>>
    {

        private readonly IBaseRepository<Physicalexamination> Physicalexaminationrepository;
        private readonly IBaseRepository<Patient> Patientrepository;
        private readonly IBaseRepository<HealthCheckPackage> HealthCheckPackagerepository;

        public GetPhysicalexaminationHarder(IBaseRepository<Physicalexamination> physicalexaminationrepository, IBaseRepository<Patient> patientrepository, IBaseRepository<HealthCheckPackage> healthCheckPackagerepository)
        {
            Physicalexaminationrepository = physicalexaminationrepository;
            Patientrepository = patientrepository;
            HealthCheckPackagerepository = healthCheckPackagerepository;
        }

        public Task<APIPaging<PhysicalexaminationDto>> Handle(GetPhysicalexaminationCommand request, CancellationToken cancellationToken)
        {
            APIPaging<PhysicalexaminationDto> result = new APIPaging<PhysicalexaminationDto>();
            result.Code = APIEnum.Mvc;
            result.Msg = "查询成功";

            var list = from a in Physicalexaminationrepository.GetAll()
                       join b in Patientrepository.GetAll() on a.PatientId equals b.PatientId
                       join c in HealthCheckPackagerepository.GetAll() on a.HealthCheckPackageId equals c.HealthCheckPackageId
                       where a.IsDeleted == false
                       select new PhysicalexaminationDto
                       {

        physicalId=a.physicalId,

        /// <summary>
        /// 体检人
        /// </summary>
       PatientId =b.PatientId,

        /// 
        /// <summary>
        /// 关系
        /// </summary>
        Impact=b.Impact,
        /// <summary>
        /// 姓名
        /// </summary>
         PatientName =b.PatientName,
        /// <summary>
        /// 证件类型
        /// </summary>
         DocumentType =b.DocumentType,
        /// <summary>
        /// 证件号码（身份证号）
        /// </summary>
         DocumentNo =b.DocumentNo,
        /// <summary>
        /// 就诊卡号
        /// </summary>
        PatientNo =b.PatientNo,
        /// <summary>
        /// 手机号码
        /// </summary>
         Phone=b.PatientNo,
        /// <summary>
        /// 就诊人地址（选填）
        /// </summary>
        Address =b.Address,
        /// <summary>
        /// 设为默认就诊人
        /// </summary>
      IsDefault =b.IsDefault,
        /// <summary>
        /// 绑定Id
        /// </summary>
       ParentId =b.ParentId,

        /// <summary>
        /// 医院
        /// </summary>
       Hospital =a.Hospital,
        /// <summary>
        /// 地址
        /// </summary>
       Address2 =a.Address,

        /// <summary>
        /// 科室
        /// </summary>
       Department=a.Department,

        /// <summary>
        /// 预约体检时间
        /// </summary>
         PhysicalTime=a.PhysicalTime,

        /// <summary>
        /// 候诊时间
        /// </summary>
       WaitingTime=a.WaitingTime,

        /// <summary>
        /// 套餐
        /// </summary>
      HealthCheckPackageId=c.HealthCheckPackageId,


        /// <summary>
        /// 体检套餐名称
        /// </summary>
         PackageName =c.PackageName,

        PackageType=c.PackageType,
        /// <summary>
        /// 价格
        /// </summary>
         Medicalexaminationfee =c.PackagePrice,

        /// <summary>
        /// 预约时间
        /// </summary>
       bookingTime=a.bookingTime,

        /// <summary>
        /// 状态
        /// </summary>
     State=a.State,

                           Checktime = a.Checktime,
                           /// <summary>
                           /// 备注
                           /// </summary>
                           Remark =a.Remark


                       };


            if (request.PatientId !=null)
            {
                list = list.Where(x => x.PatientId == request.PatientId);
            }

            if (!string.IsNullOrEmpty(request.State))
            {
                list = list.Where(x => x.State == request.State);
            }

            if (request.CreateTime != null)
            {
                list = list.Where(x => x.PhysicalTime>= request.CreateTime);
            }

            if (request.EndTime != null)
            {
                list = list.Where(x => x.PhysicalTime <= request.EndTime);
            }

            result.Count = list.Count();
            result.PageCount = (int)Math.Ceiling(result.Count * 1.0 / request.PageSize);
            result.Data = list.OrderByDescending(a => a.physicalId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            return Task.FromResult(result);
        }
    }
}
